package com.financing.wap.web.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.financing.wap.common.ResultMap;
import com.financing.wap.service.IDstUserArticleCategoriesService;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.UserArticleInfoCategoriesVO;
import com.financing.wap.web.controller.BaseController;

@Controller("dstUserArticleCategoriesController")
@RequestMapping("/api/dstUserArticleCategories")
public class DstUserArticleCategoriesController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstUserArticleCategoriesService dstUserArticleCategoriesService;
	
	/***
	 * 我的文章分类列表
	 * @param request
	 * @param dstUserArticleCategories
	 * @param pager
	 * @return
	 */
	@RequestMapping("/listDstUserArticleCategories")
	@ResponseBody
	public ResultMap list(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query dstUserArticleCategories list begin .......");
			
			String openid = request.getParameter("openid");
			if(StringUtil.isNullOrEmptyStr(openid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			String flagConcern = request.getParameter("flagConcern");
			
			List<UserArticleInfoCategoriesVO> list = dstUserArticleCategoriesService.queryAllCategoriesByOpenId(openid);
			
			boolean flag=true;
			for (int i = 0; i < list.size(); i++) {
				if("2".equals(list.get(i).getIsMyType())){
					flag=false;
					break;
				}
			}
			//查出列表显示数据
			if(StringUtils.isNotBlank(flagConcern)){
				if(!flag){//存在关注  把未关注的数据移除
					for (int i = list.size()-1; i >=0; i--) {
						if(!"2".equals(list.get(i).getIsMyType())){
							list.remove(i);
						}
					}
				}
			}
			
			result.setAttribute("list",list);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query dstUserArticleCategories list end .......");
		}catch(Exception e){
			log.error("query dstUserArticleCategories error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	/***
	 * 用户编辑分类
	 * @param request
	 * @return
	 */
	@RequestMapping("/addDstUserArticleCategories")
	@ResponseBody
	public ResultMap add(HttpServletRequest request ){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add dstUserArticleCategories  begin .......");
			
			String openid=request.getParameter("openid");
			String articleTypes=request.getParameter("articleTypes");
			if(StringUtil.isNullOrEmptyStr(openid,articleTypes)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			//处理json  isMyType=2 已关注  id取出
			JSONArray jsonArray=JSONArray.parseArray(articleTypes);
			String ids="";
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				String isMyType = object.getString("isMyType");
				if("2".equals(isMyType)){
					ids+=","+object.getString("id");
				}
			}
			dstUserArticleCategoriesService.batchUpdate(openid, ids);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add dstUserArticleCategories list end .......");
		}catch(Exception e){
			log.error("add dstUserArticleCategories error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
